/* 
 * File:   runtime.h
 * Author: lin
 *
 * Created on May 8, 2011, 11:14 AM
 */

#ifndef RUNTIME_H
#define	RUNTIME_H

#include "backend.h"
#include "execution.h"
#include "common.h"
#include "runtime.c"


/* statement executions*/
enum execution_cate {
        EXE_RETURN,
        EXE_BREAK,
        EXE_CONTINUE,
        EXE_ERROR,
        EXE_FINISHED
};

enum evaluation_cate {
        VALUE_INT,
        VALUE_BOOL,
        VALUE_ENUM_BASIC,
        ENUM_VALUE_COMBO,

        VARIABLE
};


struct data_enum_basic {
    int type;
    int index;
    int value; /* unmodifieable */
};

struct variable {

        struct type _type;
        union {
                int* value;

                struct data_enum_basic *address;

                //struct array_variable *address;

                //struct container *address;

        } spec;

};

/* expression evaluation and type inferences */
struct evaluation {
        enum evaluation_cate cate;

        union  {
                int value;
                struct variable _variable;
        } spec;
};

struct execution {
        struct evaluation evaluation;
        enum execution_cate cate;
};



struct evaluation INVALID_EVALUATION;
struct evaluation DEFAULT_EVALUATION;

struct execution INVALID_EXECUTION;
struct execution DEFAULT_EXECUTION;

int value_of(struct variable v);
struct type access_variable_type(char* variable, struct access_table *table);
struct variable access_variable(char* variable, struct access_table *table);
struct data_enum_basic test1;

int test;
int i = 0;
int j = 0;
int condition = 0;


struct access_table *append_access_table(char* variable, struct access_table *table){
    return NULL;
}

int value_of(struct variable v)  {

    switch(v._type.cate) {
        case INTEGER:
        case BOOLEAN:
            return (*v.spec.value);

        default:
            return (v.spec.address->value);
    }
}

struct variable access_variable(char* variable, struct access_table *table) {
    struct variable v;
    v._type = access_variable_type(variable, table);

    if (strcmp(variable, "test") == 0) {

        v.spec.value = &test;
    }

    else if( strcmp(variable, "i") == 0) {
        v.spec.value = &i;

    }

    else if( strcmp(variable, "j") == 0) {
        v.spec.value = &j;
    }

    else if( strcmp(variable, "condition") == 0) {
        v.spec.value = &condition;
    }
    return v;
}

struct type access_variable_type(char* variable, struct access_table *table){

    struct type _type;
    _type.cate = INTEGER;
    return _type;
}


#endif	/* RUNTIME_H */

